<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>用户登录</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .form-container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .form-toggle {
            margin-top: 15px;
            text-align: center;
        }
        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <!-- 登录表单 -->
        <form id="loginForm" action="${pageContext.request.contextPath}/logintest" method="post">
            <!-- 在登录表单上方添加错误提示 -->
            <c:if test="${not empty param.error}">
                <div class="alert alert-danger">
                    用户名或密码错误
                </div>
            </c:if>
            <h2 class="text-center mb-4">用户登录</h2>
            <div class="mb-3">
                <label for="loginUsername" class="form-label">用户名</label>
                <input type="text" class="form-control" id="loginUsername" name="username" required>
            </div>
            <div class="mb-3">
                <label for="loginPassword" class="form-label">密码</label>
                <input type="password" class="form-control" id="loginPassword" name="password" required>
            </div>
            <button type="submit" class="btn btn-primary w-100">登录</button>
            <div class="form-toggle">
                <button type="button" class="btn btn-link" onclick="showRegisterForm()">去注册</button>
            </div>
        </form>

        <!-- 注册表单 -->
        <form id="registerForm" class="hidden" action="${pageContext.request.contextPath}/register" method="post">
            <h2 class="text-center mb-4">用户注册</h2>
            <div class="mb-3">
                <label for="registerUsername" class="form-label">用户名</label>
                <input type="text" class="form-control" id="registerUsername" name="username" required>
            </div>
            <div class="mb-3">
                <label for="registerPassword" class="form-label">密码</label>
                <input type="password" class="form-control" id="registerPassword" name="password" required>
            </div>
            <div class="mb-3">
                <label for="confirmPassword" class="form-label">确认密码</label>
                <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required>
            </div>
            <button type="submit" class="btn btn-success w-100">注册</button>
            <div class="form-toggle">
                <button type="button" class="btn btn-link" onclick="showLoginForm()">去登录</button>
            </div>
        </form>
    </div>

    <script>
        function showRegisterForm() {
            document.getElementById('loginForm').classList.add('hidden');
            document.getElementById('registerForm').classList.remove('hidden');
        }

        function showLoginForm() {
            document.getElementById('registerForm').classList.add('hidden');
            document.getElementById('loginForm').classList.remove('hidden');
        }
    </script>
</body>
</html>